// ignore_for_file: unused_local_variable

import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:time_manager/utils/DataBaseRecord.dart';
import 'package:time_manager/utils/RecordModel.dart';
import 'package:time_manager/utils/timeDeal.dart';

class CurrentController extends GetxController {
  var isLoading = true.obs;
  var list = <RecoreModel>[].obs;

  @override
  void onInit() async {
    // DataBaseRecord().deleteDb();
    await fetchRecordItem();
    super.onInit();
  }

  addRecord() async {
    final result = await Get.toNamed('/add_record');
    print(result);
    // if (result) {
    //   fetchRecordItem();
    // }
  }

  Future<void> onRefresh() async {
    await fetchRecordItem();
  }

  refreshList(int index) {
    list[index].status = '2';
    DataBaseRecord().updateRecord(list[index]);
    final temp = list[index];
    list.remove(temp);
    list.insert(index, temp);
    update();
    Get.back();
  }

  // 获取当天日程
  fetchRecordItem() async {
    var currentTime = TimeUtil().getLocalTime(
        DateTime.now().millisecondsSinceEpoch.toString(),
        format: 'hour');
    list.clear();
    final result = await DataBaseRecord().selectRecord(
        orerBy: 'createTime desc',
        where: 'dealtime=? ',
        whereArgs: [currentTime]);
    list.assignAll(result);
  }
}
